package com.yueqian.xml.myTest;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author shkstart
 * @create 2021-02-01 23:46
 */
public class ParseXMLTest<mainc> {
    public static void main(String[] args) {
        List acclist = null;
        try {
            acclist = parseAccXML("account.xml");
            System.out.println(acclist);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        }


    }

    /*
     *
     *解析xml
     * */
    public static List<Account> parseAccXML(String filePath) throws IOException, SAXException, ParserConfigurationException {
        List<Account> list = new ArrayList<>();
        //非法过滤
        File file = new File(filePath);
        if (!file.exists()) {
            return list;
        }
        //(1) 获取DOM构建器的工厂实例；
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        //(2)从工厂创建DOM构建器：
        DocumentBuilder builder = factory.newDocumentBuilder();
        //(3)使用构建器对xml文件进行解析，获取文档对象节点：
        Document document = builder.parse(new File("file"));
        //(4)从文档对象节点中得到根元素节点：
        Element root = document.getDocumentElement();
        System.out.println(root.getNodeName());
        //(5)访问节点的属性、遍历子节点
        NodeList nl = root.getChildNodes();
        for (int i = 0; i < nl.getLength(); i++) {
            Node node = nl.item(i);
            if ("acc".equals(node.getNodeName()) && node.getNodeType() == Node.ELEMENT_NODE) {
                Element accelement = (Element) node;
                Account ac = new Account();

                ac.setGender("男".equals(accelement.getAttribute("gender")));
                //获取子标签
                NodeList childNodes = accelement.getChildNodes();
                //循环获取
                for (int j = 0; j < childNodes.getLength(); j++) {
                    Node node1 = childNodes.item(j);
                    if ("name".equals(node1.getNodeName())) {
                        ac.setName(node1.getTextContent());
                    } else if ("money".equals(node1.getNodeName())) {
                        if (!checkNumber(node1.getTextContent())) {
                            throw new IllegalArgumentException();
                        }
                        ac.setMoney(Double.parseDouble(node1.getTextContent()));
                    } else if ("accDate".equals(node1.getTextContent())) {
                        if (!checkDate(node1.getTextContent())) {
                            throw new IllegalArgumentException();

                        }
                        ac.setAccDate(parseDate(node1.getTextContent()));
                    } else if ("type".equals(node1.getNodeName())) {
                        Element element = (Element) node1;
                        ac.setType(element.getAttribute("typeName"));
                    }

                }
                list.add(ac);
            }


        }
        return list;
    }

    public static boolean checkNumber(String number) {
        if (number == null || number.trim().isEmpty()) {
            return false;
        }
        return number.matches("\\d*\\.?\\d*");

    }

    public static boolean checkDate(String datestr) {
        if (datestr == null || datestr.trim().isEmpty()) {
            return false;
        }
        return datestr.matches("\\d{4}\\-\\d{1,2}\\-\\d{1,2}");

    }

    public static Date parseDate(String datestr) {
        SimpleDateFormat s1 = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = s1.parse(datestr);
            return date;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;

    }

}
